//
//  MainTabBar.swift
//  HMWeibo007
//
//  Created by TTang on 15/6/28.
//  Copyright © 2015年 shanyu. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

     //定义按钮个数
     private let buttonCount = 5
    
     override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = bounds.width /  CGFloat(buttonCount)
        let rect = CGRectMake(0, 0, w, bounds.height)
        var index = 0
        //遍历子控件
        for v in subviews {
            //判断如果不是加号按钮，设置其frame
            if v is UIControl && !(v is UIButton) {
            
               v.frame = CGRectOffset(rect, CGFloat(index) * w , 0)
               //空出加号按钮的位置
               index += index == 1 ? 2:1
            
            }
            //设置加号按钮的frame
            composedButton.frame = CGRectOffset(rect, w * 2 , 0)
        }
        
    }
    //懒加载加号按钮 
    lazy var composedButton: UIButton = {
    
        let btn = UIButton()

        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)

        return btn
    }()
    
    

}
